/*
 * @Description:
 * @Author: Rfan
 * @Date: 2022-06-21 21:37:30
 * @LastEditTime: 2022-07-02 18:15:52
 */

import { useEffect, useState } from 'react';
import { Button, Progress, Space, Table, Tag, Typography } from 'antd';
import classNames from 'classnames/bind';
import { observer } from 'mobx-react-lite';
import { useFormatMessage } from '@/utils/intlHelpers';
import { useBoxMgtContext } from '../../contexts/BoxMgtContext';
import { byteConvert, formatDate } from '@/utils';
import { IPageQuery } from '@/interfaces/global';

import styles from '../../styles/pageList.module.scss';

const cx = classNames.bind(styles);

const { Column } = Table;

const BoxTableList = (props: any) => {
  const { className } = props;
  const {
    list,
    selectItems,
    setSelectKey,
    getBoxList,
    deleteExportFunc,
    exportTypeFilter,
    stateFilter,
    stateColorFilter
  } = useBoxMgtContext();
  const formatMessage = useFormatMessage();
  const [loading, setLoading] = useState(false);
  const [total, setTotal] = useState(0);
  const [listQuery, setListQuery] = useState<IPageQuery>({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });

  useEffect(() => {
    if (listQuery) {
      setLoading(true);
      getBoxList(listQuery)
        .then((res: any) => {
          setTotal(res?.totalCount);
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [listQuery]);

  /**
   * @description 切换页码触发
   * @param {number} page 页码
   * @param {number} pageSize 每页条目数
   */
  const pageChange = (page: number, pageSize: number) => {
    setListQuery((state) => ({
      ...state,
      pageNo: page,
      pageSize
    }));
  };

  /**
   * @description : 表格选项相关设置
   * @param {number} selectedRowKeys 选择的项目的KEY
   */
  const onSelectChange = (selectedRowKeys: number[]) => {
    setSelectKey(selectedRowKeys);
  };

  /**
   * @description:  表格多选配置
   */
  const rowSelection = {
    selectItems,
    onChange: onSelectChange
  };

  return (
    <div className={className}>
      <Table
        dataSource={list}
        loading={loading}
        rowKey="id"
        scroll={{ x: 'max-content' }}
        rowSelection={rowSelection}
        bordered
        pagination={{
          current: listQuery.pageNo,
          pageSize: listQuery.pageSize,
          total,
          onChange: pageChange
        }}
      >
        <Column title="序号" dataIndex="id" key="id" align="center" />
        <Column title="年度" dataIndex="name" key="name" />
        <Column
          title="分类号"
          dataIndex="exportType"
          key="exportType"
          align="center"
          render={(text) => exportTypeFilter(text)}
        />
        <Column title="保管期限" dataIndex="archiveTypeId" key="archiveTypeId" align="center" />
        <Column
          title="分类名称"
          dataIndex="startTime"
          key="startTime"
          align="center"
          render={(text) => (text ? formatDate(text) : '')}
        />
        <Column
          title="盒号"
          dataIndex="endTime"
          key="endTime"
          align="center"
          render={(text) => (text ? formatDate(text) : '')}
        />
        <Column
          title="件数"
          dataIndex={['exportInfo', 'size']}
          key="size"
          align="center"
          render={(size) => (size ? byteConvert(size) : size)}
        />
        <Column
          title="件号起"
          dataIndex={['exportEntryInfo', 'entryIdList']}
          key="entryNum"
          align="center"
          render={(text) => text.length}
        />
        <Column
          title="件号止"
          dataIndex="status"
          key="status"
          align="center"
          render={(status: number, record: any) => (
            <>
              {record.progress && <Progress percent={record.progress} steps={5} size="small" />}
              <Tag color={stateColorFilter(status)}>{stateFilter(status)}</Tag>
            </>
          )}
        />
        <Column
          title="总页数"
          dataIndex="exportInfo"
          key="exportFile"
          render={({ name, url, deleted }, record: any) => (
            <Space>
              <Typography.Link href={url} disabled={deleted} target="_blank">
                {name}
              </Typography.Link>
            </Space>
          )}
        />
        <Column title="存放地点" dataIndex="name" key="name" />
        <Column title="背脊宽度" dataIndex="name" key="name" />
        <Column
          title={formatMessage('tableAction')}
          key="action"
          className={cx('action-col')}
          align="center"
          render={(_text, record: any) => (
            <Space>
              <Button
                type="link"
                size="small"
                danger
                onClick={() => {
                  deleteExportFunc(record.id);
                }}
              >
                删除
              </Button>
            </Space>
          )}
        />
        <Column title="报告" key="report" align="center" />
      </Table>
    </div>
  );
};

export default observer(BoxTableList);
